package poradausersparser;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Martin Mihalovic
 */
public class PoradaUsersParser {

    private static String sourceDir;
    private static String targetFile;
    private BufferedWriter writer;
    
    /**
     * @param args source directory with user profiles (HTML files) and target file where parsed
     *             users will be saved.
     */
    public static void main(String[] args) throws IOException {
        
        if(args.length == 2){
            sourceDir = args[0];
            if(!sourceDir.endsWith("\\"))
                sourceDir += "\\";
            File dir = new File(sourceDir);
            if(!dir.exists() || !dir.isDirectory()){
                System.err.println("Invalid input directory " + sourceDir);
                return;
            }
            
            targetFile = args[1];
            File target = new File(targetFile);
            if(!target.createNewFile()){
                System.out.println("File " + targetFile + " already exists. Its content will be deleted");
                new FileOutputStream(target).write('\n');       //Note: stream not closed
            }
        }
        
        PoradaUsersParser parser = new PoradaUsersParser();
        parser.parse();
        
        System.out.println("FINISHED. Press ENTER to quit");
        System.in.read();
    }
    
    
    private FileLoader fileLoader = new FileLoader(sourceDir);

    
    private void parse() throws FileNotFoundException, IOException{
        BufferedReader reader;
        writer = new BufferedWriter(new FileWriter(targetFile));
        writer.write("INSERT INTO user VALUES");
        writer.newLine();

        Pattern userId = Pattern.compile("var\\sRELPATH\\s=\\s\"member\\.php\\?u=(\\d{1,6})\";");
        Pattern userNick = Pattern.compile("Prehľad profilu::\\s?([^<]{1,150})\\s?- PORADA.sk</title>");
        
        
        while((reader = fileLoader.GetNextFile()) != null) {
            StringBuilder fileData = new StringBuilder(1000);
            char [] buffer = new char[1024];
            int numRead = 0;
            while((numRead=reader.read(buffer)) != -1)
                fileData.append(buffer, 0, numRead);

            reader.close();
            String text = fileData.toString();
            
            Matcher m = userId.matcher(text);
            int id;
            String nick;
            if(m.find())
                id = Integer.valueOf(m.group(1));
            else {
                log("Unable to find ID... skipping to next file");
                continue;
            }
            
            m = userNick.matcher(text);
            if(m.find())
                nick = m.group(1);
            else {
                log("Unable to find nick (ID="+id+")... skipping to next file");
                continue;
            }
            
            save(id, nick);
            if(counter % 30 == 0)
                System.out.println("User " + nick + "(" + id + ")");
        }
        
        writer.close();
    }
    
    
    int counter = 0;

    private void save(int id, String userNick) throws IOException {
        writer.write("(" + id + ", '" + userNick.trim().replace('\'', '`').replace("\\", "\\\\") + "')");
        counter++;
        if(counter % 150 == 0) {
            writer.newLine();
            writer.write("INSERT INTO user VALUES");
            writer.newLine();
        }
        else if (counter % 5 == 0) {
            writer.write(", ");
            writer.newLine();
        }
        else writer.write(", ");
    }
    
    private void log(String message) throws IOException{
        System.err.println(message);
        
        FileWriter fWriter = new FileWriter("UsersParser.log", true);
        BufferedWriter out = new BufferedWriter(fWriter);
        out.write(message);
        out.close();
    }            
}
